package com.bms.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @description: 响应信息主体
 * @author 姚炜琦
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "返回响应信息主体类",description = "返回响应信息主体类")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /** 成功 */
    public static final int SUCCESS = 100;
    /** 失败 */
    public static final int FAIL = 500;

    /**
     * 状态码 200:成功 其余异常
     */
    @ApiModelProperty(name="code",value = "状态码 100:成功 其余异常",dataType = "int")
    private int code;

    /**
     * 自定义信息
     */
    @ApiModelProperty(name="msg",value = "自定义信息",dataType = "String")
    private String msg;

    /**
     * 返回数据主体
     */
    @ApiModelProperty(name="data",value = "返回数据主体",dataType = "T")
    private T data;
    public static <T> Result<T> success() {
        return restResult(null, SUCCESS, "操作成功");
    }
    public static <T> Result<T> success(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }
    public static <T> Result<T> success(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }
    public static <T> Result<T> error() {
        return restResult(null, FAIL, "操作失败");
    }
    public static <T> Result<T> error(String msg) {
        return restResult(null, FAIL, msg);
    }
    public static <T> Result<T> error(T data) {
        return restResult(data, FAIL, "操作失败");
    }
    public static <T> Result<T> error(T data, String msg) {
        return restResult(data, FAIL, msg);
    }
    public static <T> Result<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }
    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
